﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Checkers
{
    public partial class Form1 : Form
    {
        internal CheckerBoard checkerBoard { get; set; }
        internal CheckerBoardGUI checkerBoardGUI { get; set; }

        public Form1()
        {
            InitializeComponent();
            InitializeGame();
        }

        private void InitializeGame()
        {
            InitializeCheckerBoard();
            UpdateForm();
        }

        private void InitializeCheckerBoard()
        {
            this.checkerBoard = new CheckerBoard();
            this.checkerBoardGUI = new CheckerBoardGUI(this.checkerBoard, this);
            this.UpdatePlayerTypes();

            this.checkerBoardArea.Controls.Clear();
            this.checkerBoardArea.Controls.Add(this.checkerBoardGUI);
        }

        //
        // If any bells and whistles need to be updated, add here
        internal void UpdateForm()
        {
            this.UpdatePlayerTurn();
            this.UpdateRemainingPieces();
            this.UpdateValueLabel();
        }

        private void UpdateRemainingPieces()
        {
            Tuple<int, int> remainingPieces = GetRemainingPieces();
            bool endOfGame = false;
            string winner = string.Empty;

            this.label6.Text = string.Format("{0}", remainingPieces.Item1);
            this.label7.Text = string.Format("{0}", remainingPieces.Item2);

            if (remainingPieces.Item1 == 0)
            {
                winner = "red";
                endOfGame = true;
            }
            else if (remainingPieces.Item2 == 0)
            {
                winner = "black";
                endOfGame = true;
            }

            if (endOfGame)
            {
                MessageBox.Show(string.Format("End of Game!\nThe winner is {0}.\nA new game will now start", winner));
                InitializeGame();
            }
        }

        private Tuple<int, int> GetRemainingPieces()
        {
            CheckerPiece[,] board = (CheckerPiece[,])checkerBoard.checkerBoard.Clone();
            int red = 0;
            int black = 0;

            foreach (CheckerPiece piece in board)
            {
                if (piece == CheckerPiece.BlackKing || piece == CheckerPiece.BlackReg)
                    ++black;
                else if (piece == CheckerPiece.RedKing || piece == CheckerPiece.RedReg)
                    ++red;
            }

            return new Tuple<int, int>(red, black);
        }
        //
        // You must change the current player's turn via the checkerBoard, then call this method
        private void UpdatePlayerTurn()
        {
            if (this.checkerBoard.currentPlayersTurn == Player.Black)
            {
                this.label2.Text = "Black";
                this.pictureBox1.BackColor = Color.Black;
            }
            else
            {
                this.label2.Text = "Red";
                this.pictureBox1.BackColor = Color.Red;
            }
        }

        private void UpdatePlayerTypes()
        {
            if (this.playerToolStripMenuItem0.Checked)
                this.checkerBoardGUI.UpdatePlayerType(TuringTest.AI, TuringTest.AI);
            else if (this.playerToolStripMenuItem1.Checked)
                this.checkerBoardGUI.UpdatePlayerType(TuringTest.HUMAN, TuringTest.AI);
            else
                this.checkerBoardGUI.UpdatePlayerType(TuringTest.HUMAN, TuringTest.HUMAN);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string aboutMessage = "Created by:\n" +
                "\tMiguel Molina Jr.\n" + 
                "\tThomas Furman\n" + 
                "\tJoshua Yount";

            MessageBox.Show(this, aboutMessage, "About");
        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.InitializeGame();
        }

        private void playerToolStripMenuItem0_Click(object sender, EventArgs e)
        {
            this.playerToolStripMenuItem0.Checked = false;
            this.playerToolStripMenuItem1.Checked = false;
            this.playerToolStripMenuItem2.Checked = false;

            ToolStripMenuItem selectedMenuItem = (ToolStripMenuItem)sender;
            selectedMenuItem.Checked = true;

            this.UpdatePlayerTypes();
            //
            // After updating player types, need to perform a move if necessary; if AI player's move
            this.checkerBoardGUI.HandleAIMove();
        }

        private void trackBar1_ValueChanged(object sender, EventArgs e)
        {
            UpdateValueLabel();
        }

        private void UpdateValueLabel()
        {
            this.label9.Text = string.Format("{0}", this.trackBar1.Value);
            this.checkerBoardGUI.aiDepth = this.trackBar1.Value;
        }
    }
}
